package com.gzy.wbapplication.modules.log;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.gzy.wbapplication.R;
import com.gzy.wbapplication.events.ReloadLogEvent;
import com.gzy.wbapplication.manager.AuthManager;
import com.gzy.wbapplication.model.MyLog;
import com.gzy.wbapplication.model.Result;
import com.gzy.wbapplication.net.LogApi;
import com.gzy.wbapplication.net.core.XXModelCallback;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;

public class AddLogActivity extends AppCompatActivity {

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_log);

        findViewById(R.id.rl_back).setOnClickListener((v)->{
            finish();
        });

        findViewById(R.id.tv_commit).setOnClickListener((v)->{
            commit();
        });

        editText = findViewById(R.id.editText);
    }

    private void commit() {
        String text = editText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(this, "日志内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        LogApi.addLog(AuthManager.loadToken(this), text, new XXModelCallback<Result<MyLog>>(new TypeReference<Result<MyLog>>(){}) {
            @Override
            public void onResponseData(Call call, Result<MyLog> model) {
                Toast.makeText(AddLogActivity.this, "创建成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new ReloadLogEvent());
                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(AddLogActivity.this, "创建失败：" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
